package org.jeecg.modules.submodules.mapper;

import io.netty.util.internal.StringUtil;
import org.jeecg.modules.submodules.entity.vo.UserCarVo;

public class CarMapperProvider {


    public String getListOfUserCarVo(UserCarVo carVo){
        String sql = "SELECT\n" +
                "\tcar.id carId,\n" +
                "\tcar.CAR_NUMBER,\n" +
                "\tcar.CAR_FRAME_NUMBER,\n" +
                "\tcar.CAR_HITCH_NUMBER,\n" +
                "\tcar.ANNUAL_CHECK_TIME,\n" +
                "\tcar.BUSINESS_INSURANCE_TIME,\n" +
                "\tcar.TRAFFIC_INSURANCE_TIME,\n" +
                "\tcar.CAR_STATUS,\n" +
                "\tsu.id userId,\n" +
                "\tsu.realname defaultDriver,\n" +
                "\tGROUP_CONCAT( sub.id ) beforeDriverId,\n" +
                "\tGROUP_CONCAT( sub.realname ) beforeDriver \n" +
                "FROM\n" +
                "\tCar car\n" +
                "\tLEFT JOIN user_car uc ON uc.CAR_ID = car.id\n" +
                "\tLEFT JOIN ( SELECT\n" +
                "\tsu.id,\n" +
                "\tsu.realname \n" +
                "FROM\n" +
                "\tsys_user su\n" +
                "\tLEFT JOIN user_car uc ON uc.USER_ID = su.id\n" +
                "\tLEFT JOIN car ON car.id = uc.CAR_ID \n" +
                "WHERE\n" +
                "\tuc.IS_DEFAULT = 1 \n" +
                "\t) su ON su.id = uc.USER_ID\n" +
                "\tLEFT JOIN ( SELECT\n" +
                "\tsu.id,\n" +
                "\tsu.realname \n" +
                "FROM\n" +
                "\tsys_user su\n" +
                "\tLEFT JOIN user_car uc ON uc.USER_ID = su.id\n" +
                "\tLEFT JOIN car ON car.id = uc.CAR_ID \n" +
                "WHERE\n" +
                "\tuc.IS_DEFAULT = 0 ) sub ON sub.id = uc.USER_ID \n" ;
        StringBuilder builder = new StringBuilder(sql);
        builder.append("\n where 1=1 \n");
        if (!StringUtil.isNullOrEmpty(carVo.getCarNumber())){
            builder.append("AND \tcar.CAR_NUMBER like '%"+carVo.getCarNumber()+"%'\n");
        }
        if (!StringUtil.isNullOrEmpty(carVo.getDefaultDriver())){
            builder.append("AND \t su.realname like  '%"+carVo.getDefaultDriver()+"%'\n");
        }
        if (!StringUtil.isNullOrEmpty(carVo.getBeforeDriver())){
            builder.append("AND \t sub.realname like  '%"+carVo.getBeforeDriver()+"%'\n");
        }
        builder.append("\nGROUP BY\n" +
                "\tcar.ID");
        return builder.toString();
    }
    public String getCarAndDefaultDriver(){
        String sql = "select \n" +
                "car.id carId,\n" +
                "car.CAR_NUMBER," +
                "car.CAR_STATUS,\n" +
                "su.id userId,\n" +
                "su.realname\n" +
                "from\n" +
                "car\n" +
                "left join user_car uc on uc.CAR_ID = car.ID\n" +
                "left join sys_user su on su.id = uc.USER_ID\n" +
                "left join sys_user_post sup on sup.USER_ID = su.id\n" +
                "left join sys_position sp on sp.id = sup.POST_ID\n";
        return sql;
    }
}
